<template>
  <div class="channel-edit">
    <!-- 我的频道 -->
    <van-cell center :border="false">
      <div slot="title" class="channel-title">我的频道</div>
      <van-button
        type="danger"
        plain
        round
        size="mini"
        @click="isEdit = !isEdit"
        >{{ isEdit ? "完成" : "编辑" }}</van-button
      >
    </van-cell>

    <van-grid :gutter="10">
      <van-grid-item
        :icon="isEdit && index != 0 ? 'clear' : ''"
        v-for="(channer, index) in usersChannels"
        :key="channer.id"
        :text="channer.name"
        @click="onUserChannerClick(channer, index)"
      />
    </van-grid>

    <!-- 频道推荐 -->
    <van-cell center :border="false">
      <div slot="title" class="channel-title">频道推荐</div>
    </van-cell>

    <van-grid :gutter="10">
      <van-grid-item
        v-for="item in recommendChannels"
        :key="item.id"
        :text="item.name"
        @click="onAdd(item)"
      />
    </van-grid>
  </div>
</template>
<script>
import {
  getAllchannels,
  addUserChannel,
  deleteUserChannerl,
} from "@/api/login";
import { mapState } from "vuex";
import { setItem } from "@/utils/storage";
export default {
  name: "Channel-edit",
  props: {
    usersChannels: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      allChannerls: [], //所有频道数据列表
      isEdit: false,
    };
  },
  computed: {
    ...mapState(["user"]),
    //   频道推荐 = 所有频道 - 我的频道
    recommendChannels() {
      return this.allChannerls.filter((channel) => {
        return !this.usersChannels.find((usersChannels) => {
          return usersChannels.id === channel.id;
        });
      });
    },
  },
  created() {
    this.loadAllChannerls();
  },
  methods: {
    //   获取所有频道的数据
    async loadAllChannerls() {
      const { data } = await getAllchannels();
      this.allChannerls = data.data.channels;
    },
    // 删除我的频道
    onUserChannerClick(channer, index) {
      if (this.isEdit && index != 0) {
        // 编辑状态，删除频道
        this.deleteChannel(channer, index);
      } else {
        // 非编辑状态，切换频道
        this.switchChannel(channer, index);
      }
    },
    //  编辑状态，删除频道
    deleteChannel(channer, index) {
      // 如果删除的是当前激活频道之前的频道
      if (index <= this.active) {
        // 更新激活频道的索引
        this.$emit("update-active", this.active - 1);
      }
      this.usersChannels.splice(index, 1);
      //   数据持久化
      if (this.user) {
        //   登录了，持久化到线上
        deleteUserChannerl(channer.id);
      } else {
        //   没有登录，持久化到本地
        setItem("user-channels", this.usersChannels);
      }
    },
    // 非编辑状态，切换频道
    switchChannel(channer, index) {
      this.$emit("update-active", index);
      this.$emit("close");
    },
    // 频道的添加
    async onAdd(item) {
      this.usersChannels.push(item);
      // 数据持久化
      if (this.user) {
        addUserChannel({
          channels: [
            {
              id: item.id,
              seq: this.usersChannels.length,
            },
          ],
        });
      } else {
        // 没有登录，数据存储到本地
        setItem("user-channel", this.usersChannels);
      }
    },
  },
};
</script>
<style lang="less" scoped>
.channel-edit {
  padding-top: 54px;
  .channel-title {
    font-size: 16px;
    color: #333;
  }
  .van-grid-item {
    width: 80px;
    height: 43px;
    /deep/ .van-grid-item__content {
      background-color: #f4f5f6;
      border-radius: 3px;
      border: none;
      font-size: 14px;
      color: #222;
      .van-grid-item__text {
        margin-top: 0;
      }
    }
    /deep/ .van-grid-item__icon {
      position: absolute;
      right: -5px;
      top: -5px;
      font-size: 20px;
      color: #ccc;
    }
  }

  .active {
    /deep/ .van-grid-item__text {
      color: red !important;
    }
  }
}
</style>
